﻿using AutoMapper;
using CSRedis;
using MediatR;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using SmartApplication.Api.Applient.Command.Source;
using SmartApplication.Api.Dto;
using SmartApplication.Api.Ext;
using SmartApplication.ApiCode;
using SmartApplication.Domain.Entity;
using SmartApplication.Infrastructure.Instrument;

namespace SmartApplication.Api.Applient.CommandHandler.Source
{
    /// <summary>
    /// 分页显示溯源数据信息
    /// </summary>
    public class ListSourceHandler : IRequestHandler<ListSourceCommand, ApiPaging<List<SourceDto>>>
   {
        public ListSourceHandler(IBaseRepository<SourceModel> sourceRepository,IMapper mapper)
        {
            SourceRepository = sourceRepository;
            Mapper = mapper;
        }

        public IBaseRepository<SourceModel> SourceRepository { get; }
        public IMapper Mapper { get; }

        /// <summary>
        /// 显示溯源数据信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiPaging<List<SourceDto>>> Handle(ListSourceCommand request, CancellationToken cancellationToken)
        {
            var res=new ApiPaging<List<SourceDto>>();
            
            var list = SourceRepository.GetAll();

            if (!string.IsNullOrEmpty(request.ShopName))
            {
                list = list.Where(x => x.ShopName.Contains(request.ShopName));
            }
            if (!string.IsNullOrEmpty(request.Production))
            {
                list = list.Where(x => x.Production == request.Production);
            }
            if (request.Expiration != 0)
            {
                list = list.Where(x => x.Expiration == request.Expiration);
            }
            if (!string.IsNullOrEmpty(request.ExpNumber))
            {
                list = list.Where(x => x.ExpNumber == request.ExpNumber);
            }
            if (!string.IsNullOrEmpty(request.Enterprise))
            {
                list = list.Where(x => x.Enterprise == request.Enterprise);
            }
           

            res.Totalcount =await list.CountAsync();
            res.Totalpage =  (int)Math.Ceiling(res.Totalcount* 1.0 / request.Pagesize);
            List<SourceModel>? paginglist = await list.OrderByDescending(x => x.CreateTime).Skip((request.Pageindex - 1) * request.Pagesize).Take(request.Pagesize).ToListAsync();
            var sou = Mapper.Map<List<SourceDto>>(paginglist);
            res.Code = EnumCode.Success;
            res.Message = "显示溯源数据成功";
            res.Data = sou;
            return res;
            
            //var result = RedisHelper.Get("rediskey");
            //if(result==null)
            //{
            //    var souce= sou;
            //    var json = souce.ToJson();
            //    RedisHelper.Set("rediskey", json);
            //    res.Data = souce;
            //    return res;
            //}
            //else
            //{
            //    res.Data = result.ToObject<List<SourceDto>>();
            //    return res;
            //}
        }
    }
}
